package com.content.scaffold.common.config;


import com.content.scaffold.common.client.base.CloudClientService;
import com.content.scaffold.common.client.base.CloudClientWithCacheService;
import com.content.scaffold.common.client.proxy.NotaryCloudClientAdvise;
import com.content.scaffold.common.client.proxy.NotaryCloudClientPointCut;
import com.content.scaffold.common.constants.DingTalkConstant;
import com.content.scaffold.common.constants.NotaryCloudConstant;
import com.content.scaffold.common.dingtalk.DingTalkRobot;
import com.content.scaffold.common.utils.RedisUtils;
import com.notarycloud.client.NotaryCloudClient;
import com.notarycloud.client.NotaryCloudClientConfig;
import com.notarycloud.client.NotaryCloudCredentials;
import jakarta.annotation.Resource;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author xiaojl
 */
@Configuration
public class CommonConfigBean {

    @Resource
    NotaryCloudConstant notaryCloudConstant;

    @Bean
    public NotaryCloudClient notaryCloudClient() {
        NotaryCloudClientConfig clientConfig = NotaryCloudClientConfig
                .defaultBuilder()
                .baseUrl(notaryCloudConstant.getBaseUrl())
                .build();
        NotaryCloudCredentials cloudCredentials = new NotaryCloudCredentials(notaryCloudConstant.getAppId(),
                notaryCloudConstant.getAppSecret());

        return new NotaryCloudClient(cloudCredentials, clientConfig);
    }


    @Bean("notaryCloudClientWithCache")
    public NotaryCloudClient notaryCloudClientWithCache(RedisUtils redisUtils) {

        NotaryCloudCredentials cloudCredentials =
                new NotaryCloudCredentials(notaryCloudConstant.getAppId()
                        , notaryCloudConstant.getAppSecret());
        NotaryCloudClientConfig config = NotaryCloudClientConfig
                .defaultBuilder()
                .baseUrl(notaryCloudConstant.getBaseUrl())
                .build();
        NotaryCloudClient notaryCloudClient = new NotaryCloudClient(cloudCredentials, config);
        ProxyFactory cloudClientProxy = new ProxyFactory();
        cloudClientProxy.setTarget(notaryCloudClient);
        NotaryCloudClientAdvise advise = new NotaryCloudClientAdvise();
        Advisor advisor = new DefaultPointcutAdvisor(new NotaryCloudClientPointCut(), advise);
        cloudClientProxy.addAdvisor(advisor);
        advise.setRedisUtils(redisUtils);
        return (NotaryCloudClient) cloudClientProxy.getProxy();

    }

    @Bean
    public CloudClientService cloudService(NotaryCloudClient notaryCloudClient) {
        return new CloudClientService(notaryCloudClient);
    }

    @Bean
    public CloudClientWithCacheService cloudClientWithCacheService(
            @Qualifier("notaryCloudClientWithCache") NotaryCloudClient notaryCloudClientWithCache) {
        return new CloudClientWithCacheService(notaryCloudClientWithCache);
    }

    @Resource
    DingTalkConstant dingTalkConstant;

    /**
     * 创建钉钉提醒bean
     */
    @Bean
    public DingTalkRobot dingTalkRobot() {
        return DingTalkRobot
                .builder()
                .secret(dingTalkConstant.getSecret())
                .accessToken(dingTalkConstant.getAccessToken())
                .build();
    }
}
